import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    token: localStorage.getItem('token') || '',
    userInfo: {}
  },
  getters: {
  },
  mutations: {
    updateToken(state, newToken) {
      state.token = newToken
      console.log(state.token);
      localStorage.setItem('token', newToken)
    },
    updateUserInfo(state, newUserInfo) {
      state.userInfo = newUserInfo
    }
  },
  actions: {
    // 2.定义初始化用户基本信息的action函数
    async initUserInfo(context) {
      // console.log(context);
      const { data: res } = await axios.get('/my/userinfo')
      console.log(res.data);
      if (res.code === 0) context.commit('updateUserInfo', res.data);
    }
  },
  modules: {
  }
})
